package com.gamesense.api.util.world;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.potion.Potion;

public class MotionUtils{
	public static boolean isMoving(EntityLivingBase entity){
		return entity.moveForward != 0 || entity.moveStrafing != 0;
	}

	public static void setSpeed(final EntityLivingBase entity, final double speed){
		double[] dir = forward(speed);
		entity.motionX = dir[0];
		entity.motionZ = dir[1];
	}

	public static double getBaseMoveSpeed(){
		double baseSpeed = 0.2873;
		if (Minecraft.getMinecraft().player != null && Minecraft.getMinecraft().player.isPotionActive(Potion.getPotionById(1))){
			final int amplifier = Minecraft.getMinecraft().player.getActivePotionEffect(Potion.getPotionById(1)).getAmplifier();
			baseSpeed *= 1.0 + 0.2 * (amplifier + 1);
		}
		return baseSpeed;
	}

	public static double[] forward(final double speed){
		float forward = Minecraft.getMinecraft().player.movementInput.moveForward;
		float side = Minecraft.getMinecraft().player.movementInput.moveStrafe;
		float yaw = Minecraft.getMinecraft().player.prevRotationYaw + (Minecraft.getMinecraft().player.rotationYaw - Minecraft.getMinecraft().player.prevRotationYaw) * Minecraft.getMinecraft().getRenderPartialTicks();
		if (forward != 0.0f){
			if (side > 0.0f){
				yaw += ((forward > 0.0f) ? -45 : 45);
			} else if (side < 0.0f){
				yaw += ((forward > 0.0f) ? 45 : -45);
			}
			side = 0.0f;
			if (forward > 0.0f){
				forward = 1.0f;
			} else if (forward < 0.0f){
				forward = -1.0f;
			}
		}
		final double sin = Math.sin(Math.toRadians(yaw + 90.0f));
		final double cos = Math.cos(Math.toRadians(yaw + 90.0f));
		final double posX = forward * speed * cos + side * speed * sin;
		final double posZ = forward * speed * sin - side * speed * cos;
		return new double[]{posX, posZ};
	}
}
